package com.binvshe.binvshe.opus;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.binvshe.binvshe.app.home.adapter.HomeAdapter;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlBase;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.contacts.SearchFriendActivity;
import com.binvshe.binvshe.entity.HomeEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;

/**
 * 搜索结果
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-10-31
 * @Version: 1.0.0
 */
public class SearchOpusResultFragment extends SwipeListFragment {

    private int mPage = 0;

    public static SearchOpusResultFragment newInstance(int flag) {
        Bundle args = new Bundle();
        args.putInt(SearchFriendActivity.KEY_FLAG, flag);
        SearchOpusResultFragment fragment = new SearchOpusResultFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        flag = getArguments().getInt(SearchFriendActivity.KEY_FLAG);
//        SLog.e("vivi", "flag == " + flag);

        showToolbar();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
//        ((SearchResultAdapter) getAdapter()).setFlag(flag);
//        setState(CompState.EMPTY);
        setPullToRefreshEnable(false);
        setAutoLoading(false);
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new HomeAdapter(getContext());
    }


    private ListDataRequestListener<HomeEntity> mResponceListener = new ListDataRequestListener<>(this, HomeEntity.class);

    @Override
    public void onStartLoading() {
        if (!TextUtils.isEmpty(mKey)) {
            if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
                mPage++;
                MyHttpRequest request = new MyHttpRequest(getContext());
                UrlParams urlParams = new UrlParams();
                urlParams.put(UrlBase.ParamKey.PAGE, mPage + "");
                urlParams.put("keyword", mKey);
                urlParams.put(UrlBase.ParamKey.SIZE, 20 + "");
                request.get(UrlCenter.CHANNEL_ARTICLE_LIST, urlParams, mResponceListener);

            }
        }
    }
/*
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        switch (flag) {
            // 从“我的好友”界面进入搜索界面
            case SearchFriendActivity.FLAG_MY_FRIEND:
                People people = (People) getData().get(position);
                ChatHelper.startSingleChat(getActivity(), people.getPhone(), people.getNick_name(), people.getPortrait());
                break;

            // 从“添加好友”界面进入搜索界面
            case SearchFriendActivity.FLAG_ADD_FRIEND:

                break;

        }
    }*/

    /*    @Override
    public View createEmptyView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.search_result_empty, null);
    }*/

    private String mKey;

    public void onSearch(String key) {
        if (!TextUtils.equals(mKey, key)) {
            mKey = key;
            mPage = 0;
            getData().clear();
            getAdapter().notifyDataSetChanged();
            startLoading();
        }
    }


}
